package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Laser;
import com.blb.entity.Printer;
import com.blb.entity.ProductionLine;
import com.blb.entity.User;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.mapper.PrinterMapper;
import com.blb.service.IPrinterService;
import com.blb.service.IProductionLineService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-14
 */
@Controller
@RequestMapping("/printer")
public class PrinterController {
        @Autowired
        private IPrinterService ps;


        @Autowired
        private IProductionLineService iProductionLineService;

        @Resource
        private PrinterMapper printerMapper;

    /**
     * 跳转打印机页面
     * @return
     */

     @RequestMapping("/printer")
     public  String printer(){

         return "laser/sc_dyj";
     }

    /**
     * 模糊查询
     * @param start
     * @param length
     * @param pnum
     * @return
     */

    @ResponseBody
    @RequestMapping("find")
    public BlbPage findfp(Integer start, Integer length, String pnum){
         User user = ShiroUtils.getUser();
         String companyId = user.getCompanyId();

        /*查询分页数据*/
        List<Printer> findprinter = ps.findbynum(start,length,pnum,companyId);

        /*查询总条数*/
        int count = ps.count(pnum,companyId);

        return new DataTablePage(count,count,findprinter);
    }


    @ResponseBody
    @RequestMapping("allline")
    public ResponseHelp findallline(){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<ProductionLine> productLine = iProductionLineService.list(new QueryWrapper<ProductionLine>().eq("company_id", companyId));
        System.out.println(productLine);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(productLine);
        return responseHelp;
    }

    /**
     * 添加打印机
     * @param printer
     * @return
     */
     @ResponseBody
     @RequestMapping("add")
     public ResponseHelp addPrinter( Printer printer){
         String companyId = ShiroUtils.getUser().getCompanyId();
         printer.setId(UUIDUtils.getId());
         printer.setCompanyId(companyId);
         printer.getPNum();
         ps.addPrinter(printer);
         ResponseHelp responseHelp = new ResponseHelp();
         return responseHelp;
     }
    /**
     * 删除打印机
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    private ResponseHelp delPrinter(String id){

          ps.deletePrinter(id);
        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;
    }

    /**
     * 多删
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("deletes")
    private  ResponseHelp delsPrinte(@RequestBody String[] ids){

        for (String id: ids){
             ps.deletePrinter(id);
        }

        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;
    }

    @ResponseBody
    @RequestMapping("getname")
    private ResponseHelp getprinter(String pNum){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        Printer getname = ps.getname(companyId, pNum);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(getname);
        return responseHelp;

    }

    /**
     * 通过id查询打印机
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("update")
     private  ResponseHelp findbuid(String id){
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(ps.findbyid(id));
        return responseHelp;
     }

    /**
     * 修改打印机
     * @param printer
     * @return
     */
     @ResponseBody
     @RequestMapping("update1")
     private ResponseHelp updatePrinter(Printer printer){

          ps.update(printer);
         ResponseHelp responseHelp = new ResponseHelp();

         return responseHelp;
    }



}


